//
//  ViewController.m
//  dic_array
//
//  Created by YQ05 on 2018/8/5.
//  Copyright © 2018年 YQ05. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //NSArray *array1 = @[@"abc",@"123",@"你好"];
     NSDictionary *dic3 = [[NSDictionary alloc]initWithObjectsAndKeys:@"value1",@"keya",@"value12",@"keyb",@"value3",@"keyc", nil];
    NSDictionary *dic4 = [[NSDictionary alloc]initWithObjectsAndKeys:@"value1",@"keya",@"value222",@"keyb",@"value32",@"keyc", nil];
    
    NSArray *array2 = @[dic3,dic4];
//    NSLog(@"array2 %@",array2);
//
//    for (NSString *item in array1) {
//        NSLog(@"item=%@",item);
//    }
    NSMutableArray *newarr = [[NSMutableArray alloc]init];
    
    for (NSMutableDictionary *dic in array2) {
        //NSLog(@"dic=%@",dic);
        
            NSString *item = [dic valueForKey:@"keyb"];
        if ([item isEqualToString:@"value222"]) {
            NSLog(@"item=%@",item);
            NSMutableDictionary *dic2 = [dic mutableCopy];
            [dic2 setValue:@"8" forKey:@"keyb"];
            NSLog(@"%@",dic2);
            [newarr addObject:dic2];
            
        }else{
            [newarr addObject:dic];
        }
        
        
    }
    NSLog(@"newarr  ==  %@",newarr);
    
    
    
    
    //1.要转换成JSON数据的OC对象*这里是一个字典
    NSDictionary *dictM = @{
                            @"hwm":@"是坏蛋",
                            @"age":@100,
                            @"height":@1.72
                            };
    //2.OC->JSON
    /*
     注意：可以通过+ (BOOL)isValidJSONObject:(id)obj;方法判断当前OC对象能否转换为JSON数据
     具体限制：
     1.obj 是NSArray 或 NSDictionay 以及他们派生出来的子类
     2.obj 包含的所有对象是NSString,NSNumber,NSArray,NSDictionary 或NSNull
     3.字典中所有的key必须是NSString类型的
     4.NSNumber的对象不能是NaN或无穷大
     */
    /*
     第一个参数：要转换成JSON数据的OC对象，这里为一个字典
     第二个参数：NSJSONWritingPrettyPrinted对转换之后的JSON对象进行排版，无意义
     */
    NSData *data = [NSJSONSerialization dataWithJSONObject:dictM options:kNilOptions error:nil];// OC对象 -> JSON数据 [数据传输只能以进制流方式传输,所以传输给我们的是进制流,但是本质是JSON数据
    
    NSData *data2 = [NSJSONSerialization dataWithJSONObject:newarr options:kNilOptions error:nil];
    
    NSLog(@"%@", data);
    NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    
    //3.打印查看Data是否有值
    /*
     第一个参数：要转换为STring的二进制数据
     第二个参数：编码方式，通常采用NSUTF8StringEncoding
     */
    NSLog(@"%@", data);
    NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    NSLog(@"data2  == %@", [[NSString alloc] initWithData:data2 encoding:NSUTF8StringEncoding]);
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
